home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / src / exampleCode / games / IndiZone / cycles / misc.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-02  |  6.7 KB  |  296 lines

  1.  
  2. #include <bstring.h>  /* for bcopy */
  3. #include <stdlib.h>   /* for rand, RAND_MAX */
  4. #include <math.h>     /* for sqrt */
  5. #include <gl/device.h>
  6. #include "cycles.h"
  7. #include "sound.h"
  8.  
  9. extern CYCLE *good, bike[CYCLES];
  10. extern float vec[4][2];
  11. extern float speed_fac;
  12. extern int used[CYCLES], robot[CYCLES], solo, in_win;
  13. extern int audio, demo_mode, neo_mort[CYCLES];
  14. extern POINT up_hole[LEVELS], down_hole[LEVELS];
  15.  
  16. void new_hole(POINT *);
  17. float dist(CYCLE *, CYCLE *);
  18.  
  19. /*
  20.  * vadd: returns the sum of two vectors
  21.  * note: pass two POINTS,  not pointers to POINTS
  22.  */
  23. POINT vadd (POINT a, POINT b) {
  24.     POINT temp;
  25.  
  26.     temp.x = a.x + b.x;
  27.     temp.y = a.y + b.y;
  28.     temp.z = a.z + b.z;
  29.  
  30.     return temp;
  31. }
  32.  
  33.  
  34. /*
  35.  * loop through the event queue looking for exits
  36.  * and discarding the rest
  37.  * NOTE: this is in main loop and draw_score_screen also
  38.  */
  39. void search_for_exit(void) {
  40.     short val;
  41.     int i;
  42.  
  43.     while (qtest()) {
  44.     switch(qread(&val)) {
  45.     case REDRAW:
  46.         reshapeviewport();
  47.         break;
  48.     case INPUTCHANGE:
  49.         in_win = val % 256;        /* % for dgl stuff */
  50.         break;
  51.     case ESCKEY:
  52.         good->quit = 1;
  53.         for (i = 0; i < CYCLES; i++)
  54.         if (robot[i]) bike[i].quit = 1;
  55.         if (!solo) send_update_mcast();
  56. #ifdef AUDIO
  57.         if (audio) close_audio();
  58. #endif
  59.         exit(0);
  60.         break;
  61.     default:
  62.         break;
  63.     }
  64.     }
  65. }
  66.  
  67.  
  68. /* Handles jumps for cycle C */
  69. void calcorg(CYCLE *C) {
  70.     if (C->jump) {
  71.     C->origin.y += C->jump_speed*speed_fac;
  72.     C->jump_speed -= GRAVITY*speed_fac;
  73.     if (C->origin.y <= HEIGHT) {
  74.         C->origin.y = HEIGHT;
  75.         if ((C->jump_speed = -0.2*C->jump_speed) <= 1.0)
  76.         C->jump = 0;    /* stop jumping */
  77.     }
  78.     }
  79. }
  80.  
  81.  
  82. /*if a turn has occured for a cycle (C) record it for trail drawing puposes */
  83. void newtrail(CYCLE *C) {
  84.     C->trail_ptr++;
  85.     C->trail_ptr %= TRAIL_LENGTH;
  86.     C->trail[C->trail_ptr].level = C->level; /* tag the trail as being used */
  87.     C->trail[C->trail_ptr].x = C->origin.x; /* set values */
  88.     C->trail[C->trail_ptr].y = 0.0;
  89.     C->trail[C->trail_ptr].z = C->origin.z;
  90.  
  91.     /*
  92.      * copy this turn (max one per frame) into the update
  93.      * struct for later broadcast to the other players
  94.      */
  95.     bcopy((void *)&C->trail[C->trail_ptr], (void *)&C->trail_update, (int)sizeof(POINT));
  96. }
  97.  
  98.  
  99. /* Handle a cycle turn */
  100. void turn(CYCLE *C, float dir) { 
  101.     newtrail(C);
  102.     C->vec_ptr += (int)dir;
  103.     if (C->vec_ptr == 4) C->vec_ptr = 0;
  104.     if (C->vec_ptr == -1) C->vec_ptr = 3;
  105.     C->direction.x = vec[C->vec_ptr][0];
  106.     C->direction.z = vec[C->vec_ptr][1]; 
  107. }
  108.  
  109.  
  110. /* Initiate a cycle jump */
  111. void jump(CYCLE *C) {
  112.     if (!C->jump) {
  113.     C->jump = 1;
  114.     C->jump_speed = JUMP_POWER;
  115.     }
  116. }
  117.  
  118.  
  119. /* check if a bike is outside the grid and start it falling if it is */
  120. void check_outside(CYCLE *C) {
  121.     if (C->origin.x <= -DIM || C->origin.x >= DIM
  122.      || C->origin.z <= -DIM || C->origin.z >= DIM) {
  123.     C->fall += speed_fac;
  124.     C->falling++;
  125.     C->who_we_hit = -1;
  126.     }
  127. }
  128.  
  129.  
  130. /* physically move a cycle (laterally, not vertically) */
  131. void mov(CYCLE *C) {
  132.     float scaled_step;
  133.  
  134.     scaled_step = C->step*speed_fac;
  135.     C->origin.x += scaled_step*C->direction.x;
  136.     C->origin.z += scaled_step*C->direction.z;
  137. }
  138.  
  139.  
  140. /* set the bike onto a new level */
  141. void new_level(CYCLE *C, int next_level) {
  142.     newtrail(C);
  143.     if (!solo) send_update_mcast();
  144.     C->level = next_level;
  145.     newtrail(C);
  146.     C->jump = 1;
  147.     C->origin.y = HEIGHT + 0.05*DIM;
  148.     C->jump_speed = 0.0;
  149.     if (!solo) send_update_mcast();
  150. #ifdef AUDIO
  151.     if (audio && C == good) play_sound("fall.aiff");
  152. #endif
  153. }
  154.  
  155.  
  156. /* set up one new hole */
  157. void new_hole(POINT *P) {
  158.     int size = (int)DIM;
  159.  
  160.     /* round off to nearest 10.0 */
  161.     P->x = 10.0*(int)((1.6*(float)(rand()%size) - 0.8*DIM)/10.0);
  162.     P->z = 10.0*(int)((1.6*(float)(rand()%size) - 0.8*DIM)/10.0);    
  163. }
  164.  
  165.  
  166. /* setup the init position of our holes */
  167. void init_holes(void) {
  168.     int i;
  169.  
  170.     for (i = 0; i < LEVELS; i++) {
  171.     new_hole(  &up_hole[i]);
  172.     new_hole(&down_hole[i]);
  173.     }
  174. }
  175.  
  176.  
  177. /*
  178.  * check if we've fallen through a hole into the next level
  179.  * NOTE: these holes must match up with those in draw_coloured_grid
  180.  */
  181. int fall_down_hole(CYCLE *C) {
  182.     int i, go_to;
  183.  
  184.     if (!C->jump && C->falling == 0) {  /* can't change levels whilst jumping or dying */
  185.     i = C->level;
  186.  
  187.     /* up teleport holes */
  188.     go_to = (i+LEVELS-1) % LEVELS;
  189.     if (go_to == -1) go_to = LEVELS-1;
  190.     if ( (C->origin.x > up_hole[i].x - HOLE_SIZE)
  191.       && (C->origin.x < up_hole[i].x + HOLE_SIZE)
  192.       && (C->origin.z > up_hole[i].z - HOLE_SIZE)
  193.       && (C->origin.z < up_hole[i].z + HOLE_SIZE) ) {
  194.         new_hole(&up_hole[i]);
  195.         return(go_to);
  196.     }
  197.  
  198.     /* down teleport holes */
  199.     go_to = (i+1) % LEVELS;
  200.     if ( (C->origin.x > down_hole[i].x - HOLE_SIZE)
  201.       && (C->origin.x < down_hole[i].x + HOLE_SIZE)
  202.       && (C->origin.z > down_hole[i].z - HOLE_SIZE)
  203.       && (C->origin.z < down_hole[i].z + HOLE_SIZE) ) {
  204.         new_hole(&down_hole[i]);
  205.         return(go_to);
  206.     }
  207.     }
  208.     return(-1);
  209. }
  210.  
  211.  
  212. void move_our_robots(void) {
  213.     int i;
  214.     for (i = 0; i < CYCLES; i++) {
  215.     if (robot[i]) {
  216.         if (bike[i].alive) {
  217.         if (bike[i].falling == 0)
  218.             calcorg(&bike[i]);
  219.         else if (bike[i].falling > 0) {
  220.             bike[i].fall += speed_fac;
  221.             bike[i].falling++;
  222.         }
  223.         else /* tumbling down onto grid */
  224.             tumble_down(&bike[i]);
  225.         }
  226.  
  227.         if (bike[i].fall >= WALL_FALL) {
  228.         bike[i].alive = 0;
  229.         /* restart robot */
  230.         if (!(demo_mode && i == good->id)) {
  231.             if (solo)
  232.             do {    /* loop 'til get different colour */
  233.                 init_pos(&bike[i]);
  234.             } while (bike[i].trail_colour == good->trail_colour);
  235.             else    /* don't bother if network */
  236.             init_pos(&bike[i]);
  237.  
  238.             init_tumble(&bike[i]);
  239.             if (!solo) send_full_mcast(&bike[i]);
  240.         }
  241.         }
  242.     }
  243.     }
  244. }
  245.  
  246.  
  247. /* distance between 2 bikes */
  248. float dist(CYCLE *C, CYCLE *D) {
  249.     float dx, dz;
  250.  
  251.     dx = D->origin.x - C->origin.x;
  252.     dz = D->origin.z - C->origin.z;
  253.  
  254.     return( (float)sqrt((double)(dx*dx + dz*dz)) );
  255. }
  256.  
  257.  
  258. /* do scoring here */
  259. void do_scoring(CYCLE *C, int *pts, int *big_kills, int *trail_kills) {
  260.     float distance;
  261.     int i;
  262.  
  263.     /* give us points for still being around and going fast */
  264.     *pts += (int)( (5.0 + 5.0*(C->step - MIN_STEP)/(MAX_STEP - MIN_STEP))*speed_fac );
  265.  
  266.     /*
  267.      * look at the other bikes and if they're newly starting
  268.      * to fall then maybe add stuff to our score
  269.      */
  270.     for (i = 0; i < CYCLES; i++) {
  271.     if (used[i]) {
  272.         if (!bike[i].alive)
  273.         neo_mort[i] = 0;    /* old dead */
  274.         else {
  275.         if (bike[i].falling > 0 && !neo_mort[i]) { /* they just started dying ... */
  276.  
  277.             /* see if we get points for being close */
  278.             distance = dist(C, &bike[i]);
  279.             if (bike[i].level == good->level && distance < 0.3*DIM) {
  280.             (*pts) += 10000;
  281.             (*big_kills)++;
  282.             }
  283.  
  284.             /* see if they hit our trail */
  285.             if (bike[i].who_we_hit == C->id) {
  286.             (*pts) += 3000;
  287.             (*trail_kills)++;
  288.             }
  289.  
  290.             neo_mort[i] = 1;
  291.         }
  292.         }
  293.     }
  294.     }
  295. }
  296.